package com.syezon.wifikey.pwd;

import android.arch.lifecycle.Observer;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.syezon.android.base.utils.ToastUtils;
import com.syezon.wifikey.app.WiFiKeyApp;
import com.syezon.wifikey.base.BaseLazyFragment;
import com.syezon.wifikey.R;
import com.syezon.wifikey.pwd.adapter.MyPwdAdapter;
import com.syezon.wifikey.pwd.dialog.OpListener;
import com.syezon.wifikey.pwd.dialog.OpSavePwdDialog;
import com.syezon.wifikey.pwd.info.SaveWifiPwdInfo;
import com.syezon.wifikey.pwd.viewmodel.MyPwdViewModel;
import com.syezon.wifikey.wifi.info.WifiInfo;
import com.syezon.wifikey.wifi.WifiConnectService;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MyPwdFragment extends BaseLazyFragment implements MyPwdAdapter.SavedPwdItemClickListener, OpListener {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    @BindView(R.id.rv_pwd_list)
    RecyclerView mRvPwdList;
    private MyPwdAdapter mPwdAdapter;
    private OpSavePwdDialog mOpSavePwdDialog;

    private String mParam1;
    private String mParam2;


    public MyPwdFragment() {
    }


    public static MyPwdFragment newInstance() {
        MyPwdFragment fragment = new MyPwdFragment();
        return fragment;
    }

    public static MyPwdFragment newInstance(String param1, String param2) {
        MyPwdFragment fragment = new MyPwdFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }


    @Override
    protected void onFirstUserVisible() {
        MyPwdViewModel.getInstance().getSavePwdInfoListLiveData().observe(this, new Observer<List<SaveWifiPwdInfo>>() {
            @Override
            public void onChanged(@Nullable List<SaveWifiPwdInfo> saveWifiPwdInfos) {
                if (saveWifiPwdInfos.size() > 0) {
                    toggleShowLoading(false, "");
                    mPwdAdapter.setData(saveWifiPwdInfos);
                } else {
                    showView(LayoutInflater.from(getContext()).inflate(R.layout.layout_empty_pwd, null, false), new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            MyPwdViewModel.getInstance().getSaveWifiList();
                        }
                    });
                }
            }
        });
        MyPwdViewModel.getInstance().getSaveWifiList();
    }

    @Override
    protected View getLoadingTargetView() {
        return mRvPwdList;
    }

    @Override
    protected void initViewsAndEvents() {
        mPwdAdapter = new MyPwdAdapter(getContext());
        mPwdAdapter.setListener(this);
        mRvPwdList.setLayoutManager(new LinearLayoutManager(getContext()));
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(getContext(), LinearLayoutManager.VERTICAL);
        dividerItemDecoration.setDrawable(getResources().getDrawable(R.drawable.bg_divider));
        mRvPwdList.addItemDecoration(dividerItemDecoration);
        mRvPwdList.setAdapter(mPwdAdapter);
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_my_pwd;
    }

    @OnClick({R.id.iv_add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_add:
                readyGo(AddPwdActivity.class);
                break;
        }
    }

    @Override
    public void onSavedPwdItemClick(SaveWifiPwdInfo saveWifiPwdInfo) {
        if (mOpSavePwdDialog == null) {
            mOpSavePwdDialog = new OpSavePwdDialog(getContext(), R.style.Dialog);
        }
        mOpSavePwdDialog.setSaveWifiPwdInfo(saveWifiPwdInfo);
        mOpSavePwdDialog.setOpListener(this);
        mOpSavePwdDialog.show();
    }

    @Override
    public void onDelete(SaveWifiPwdInfo saveWifiPwdInfo) {
        List<SaveWifiPwdInfo> data = mPwdAdapter.getData();
        int i = data.indexOf(saveWifiPwdInfo);
        if (i >= 0) {
            data.remove(i);
            mPwdAdapter.notifyItemRemoved(i);
            WiFiKeyApp.getDatabase().getWifiPwdDao().delete(saveWifiPwdInfo);
            WifiConnectService.getInstance().getWifiConnectionInfoMutableLiveData().postValue(WifiConnectService.getInstance().getCurrentConnectionInfo());
        }
    }

    @Override
    public void onConnect(SaveWifiPwdInfo saveWifiPwdInfo) {
        WifiInfo wifiInfo = saveWifiPwdInfo.getWifiInfo();
        if (wifiInfo != null) {
            if (wifiInfo.isConnected()) {
                ToastUtils.showShort("当前WiFi已连接！");
            } else {
                wifiInfo.setPwd(saveWifiPwdInfo.getPwd());
                WifiConnectService.getInstance().connect(wifiInfo);
            }
        } else {
            ToastUtils.showShort("不在信号范围内，无法连接该WiFi！");
        }
    }

    @Override
    public void onEdit(SaveWifiPwdInfo saveWifiPwdInfo, SaveWifiPwdInfo newInfo) {
        List<SaveWifiPwdInfo> data = mPwdAdapter.getData();
        int i = data.indexOf(saveWifiPwdInfo);
        if (i >= 0) {
            data.set(i, newInfo);
            mPwdAdapter.notifyItemChanged(i);
            WiFiKeyApp.getDatabase().getWifiPwdDao().update(newInfo);
        }
        if (data.size() == 0) {
            showView(LayoutInflater.from(getContext()).inflate(R.layout.layout_empty_pwd, null, false), new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    MyPwdViewModel.getInstance().getSaveWifiList();
                }
            });
        }
    }
}
